%Calculate the perplexity using a cross validation procedure
function perplexity = calcPerplexity(Nt,pyx)

%Calculate log P(Yn = y | Xn) 
% logError = -0.5.*sum(x*w - y).^2 - (Nt/2)*log(2*pi);
logPyx = sum(log(pyx));
%Calculate the perplexity
perplexity = exp( -1/Nt * logPyx);